package jmine.tec.web.util;

import javax.servlet.http.HttpServletRequest;

/**
 * @author gus
 */
public class FormControllerHelper {

    private final HttpServletRequest request;

    /**
     * @param request {@link HttpServletRequest}
     */
    public FormControllerHelper(final HttpServletRequest request) {
        this.request = request;
    }

    /**
     * @param parameter String
     * @return Long
     */
    public Long getParameterAsLong(final String parameter) {
        return getParameterAsLong(parameter, this.request);
    }

    /**
     * @param parameter String
     * @return Integer
     */
    public Integer getParameterAsInteger(final String parameter) {
        return getParameterAsInteger(parameter, this.request);
    }

    /**
     * Returns the specified parameter as an Integer or null if the parameter is not set.
     * 
     * @param parameter Parameter name
     * @param request {@link HttpServletRequest}
     * @return Long
     */
    public static Long getParameterAsLong(final String parameter, final HttpServletRequest request) {
        String p = request.getParameter(parameter);
        if ((p == null) || "".equals(p)) {
            return null;
        }
        try {
            return new Long(p);
        } catch (NumberFormatException e) {
            return null;
        }
    }

    /**
     * Returns the specified parameter as an Integer or null if the parameter is not set.
     * 
     * @param parameter Parameter name
     * @param request {@link HttpServletRequest}
     * @return int
     */
    public static Integer getParameterAsInteger(final String parameter, final HttpServletRequest request) {
        String p = request.getParameter(parameter);
        if ((p == null) || "".equals(p)) {
            return null;
        }
        try {
            return new Integer(p);
        } catch (NumberFormatException e) {
            return null;
        }
    }

    /**
     * @param parameter Parameter value
     * @param defaultValue the default value in case the parameter value was not found
     * @return Integer or defaultValue
     */
    public Integer getParameterAsInteger(final String parameter, final Integer defaultValue) {
        Integer in = this.getParameterAsInteger(parameter);
        return in == null ? defaultValue : in;
    }

    /**
     * Checks if the parameter is present on the request
     * 
     * @param name String parameter name
     * @return boolean
     */
    public boolean hasParameter(final String name) {
        return (this.request.getParameter(name) != null) && (this.request.getParameter(name).trim().length() > 0);
    }

    /**
     * Returns the parameter as a String. In case the parameter is not found, returns an empty String
     * 
     * @param name parameter name
     * @return String
     */
    public String getParameter(final String name) {
        return this.request.getParameter(name) == null ? "" : this.request.getParameter(name).trim();
    }

    /**
     * @param <E> the type of the service
     * @param type {@link Class}
     * @return E
     */
    public <E> E getService(final Class<E> type) {
        return HttpSessionManager.getService(this.request, type);
    }
}
